package edu.hebtu.controller;

import edu.hebtu.mapper.impl.AdminMapper;
import edu.hebtu.pojo.Admin;
import edu.hebtu.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Comparator;
import java.util.List;

@Controller
public class AdminController {

    private final AdminMapper adminMapper;

    @Autowired
    public AdminController(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @RequestMapping("/admin_info")
    public String select(Model model) {
        List<Admin> admins = adminMapper.findById(null);
        admins.removeIf(aValue -> aValue.getLevel().trim().equals("SuperAdmin"));
        for (Admin admin : admins) {
            List<Admin> adminList = adminMapper.findById(admin.getCreator());
            if (adminList.size() == 1) admin.setCreator(adminList.get(0).getName());
            if (admin.getState().trim().equals("appear")) admin.setState("正常");
        }
        model.addAttribute("admins", admins);
        return "tables/table-admin";
    }

    @RequestMapping("/admin_add")
    public String admin_add(Model model) {
        model.addAttribute("sex", 1);
        model.addAttribute("level", "Admin");
        return "add/add-admin";
    }

    @RequestMapping("/addAdmin")
    public String addAdmin(Admin admin, Model model) {

        List<Admin> admins = adminMapper.findById(null);
        boolean flag = false;
//        if (admin.getAddress() == null || admin.getAddress().equals("")) {
//            model.addAttribute("msg", "请输入家庭住址！！！");
//            flag = true;
//        }
        if (admin.getPhone() == null || admin.getPhone().trim().equals("")) {
            model.addAttribute("msg", "请输入手机号！！！");
            flag = true;
        } else if (!PhoneCheck.check(admin.getPhone().trim())) {
            model.addAttribute("msg", "手机号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getPhone().trim().equals(admin1.getPhone().trim())) {
                    model.addAttribute("msg", "手机号已存在！！！");
                    flag = true;
                }
            }
        }
        if (admin.getCard() == null || admin.getCard().trim().equals("")) {
            model.addAttribute("msg", "请输入身份证号！！！");
            flag = true;
        } else if (!IDCheck.check(admin.getCard())) {
            model.addAttribute("msg", "身份证号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getCard().trim().equals(admin1.getCard().trim())) {
                    model.addAttribute("msg", "身份证号已存在！！！");
                    flag = true;
                }
            }
        }
        if (admin.getName() == null || admin.getName().trim().equals("")) {
            model.addAttribute("msg", "请输入姓名！！！");
            flag = true;
        }

        if (flag) {
            model.addAttribute("name", admin.getName());
            model.addAttribute("sex", admin.getSex());
            model.addAttribute("card",admin.getCard());
            model.addAttribute("phone",admin.getPhone());
            model.addAttribute("address",admin.getAddress());
            model.addAttribute("level", admin.getLevel());
            return "add/add-admin";
        }

        String new_id = null;
        if(admins.size() > 0) {
            new_id = autoIdUtil.autoId(admins.get(admins.size() - 1).getAdminId(), "a");
        } else {
            new_id = autoIdUtil.autoId(null,"a");
        }
        admin.setAdminId(new_id);
        admin.setPassword("123456");
        if (admin.getLevel().equals("SuperAdmin")) admin.setPower("0");
        else admin.setPower("1101");
        if (adminMapper.add(admin) == 1) {
            model.addAttribute("msg", "添加成功，" + admin.getName() + "的账号为：" + admin.getAdminId() + "，密码为：123456。");
        } else {
            model.addAttribute("msg", "添加失败！！！");
        }
        return "add/add-admin";
    }

    @RequestMapping("/admin_edit/{adminId}")
    public String admin_edit(@PathVariable("adminId")String adminId, Model model) {
        Admin admin = adminMapper.findById(adminId).get(0);
        model.addAttribute("admin", admin);
        return "edit/edit-admin";
    }

    @RequestMapping("/editAdmin")
    public String editAdmin(Admin admin, Model model) {

        List<Admin> admins = adminMapper.findById(null);
        admins.removeIf(aValue -> aValue.getAdminId().trim().equals(admin.getAdminId().trim()));
        boolean flag = false;
//        if (admin.getAddress() == null || admin.getAddress().equals("")) {
//            model.addAttribute("msg", "请输入家庭住址！！！");
//            flag = true;
//        }
        if (admin.getPhone() == null || admin.getPhone().trim().equals("")) {
            model.addAttribute("msg", "请输入手机号！！！");
            flag = true;
        } else if (!PhoneCheck.check(admin.getPhone().trim())) {
            model.addAttribute("msg", "手机号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getPhone().trim().equals(admin1.getPhone().trim())) {
                    model.addAttribute("msg", "手机号已存在！！！");
                    flag = true;
                }
            }
        }
        if (admin.getCard() == null || admin.getCard().trim().equals("")) {
            model.addAttribute("msg", "请输入身份证号！！！");
            flag = true;
        } else if (!IDCheck.check(admin.getCard())) {
            model.addAttribute("msg", "身份证号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getCard().trim().equals(admin1.getCard().trim())) {
                    model.addAttribute("msg", "身份证号已存在！！！");
                    flag = true;
                }
            }
        }
        if (admin.getName() == null || admin.getName().trim().equals("")) {
            model.addAttribute("msg", "请输入姓名！！！");
            flag = true;
        }

        if (flag) {
            model.addAttribute("name", admin.getName());
            model.addAttribute("sex", admin.getSex());
            model.addAttribute("card",admin.getCard());
            model.addAttribute("phone",admin.getPhone());
            model.addAttribute("address",admin.getAddress());
            model.addAttribute("level", admin.getLevel());
            return "edit/edit-admin";
        }

        if (adminMapper.update(admin) == 1) {
            return "redirect:/admin_info";
        } else {
            model.addAttribute("msg", "修改失败！！！");
            return "edit/edit-admin";
        }
    }

    @RequestMapping("/admin_delete/{adminId}")
    public String admin_delete(@PathVariable("adminId")String adminId, Model model) {
        Admin admin = adminMapper.findById(adminId).get(0);
        model.addAttribute("admin", admin);
        return "delete/delete-admin";
    }

    @RequestMapping("/deleteAdmin")
    public String deleteCar(Admin admin) {
        adminMapper.delete(admin);
        return "redirect:/admin_info";
    }

    @RequestMapping("/assign_permissions/{adminId}")
    public String assign_permissions(@PathVariable("adminId") String adminId,
                                     Model model) {
        List<Admin> admins = adminMapper.findById(null);
        admins.removeIf(aValue -> aValue.getLevel().trim().equals("SuperAdmin"));   //去掉超级管理员
        admins.sort(Comparator.comparing(Admin::getName).reversed());
        model.addAttribute("admins", admins);
        if (adminId.trim().equals("null")) adminId = admins.get(0).getAdminId();
        Admin admin = adminMapper.findById(adminId).get(0);
        model.addAttribute("adminId", adminId);
        model.addAttribute("l1", String.valueOf(admin.getPower().charAt(0)));
        model.addAttribute("l2", String.valueOf(admin.getPower().charAt(1)));
        model.addAttribute("l3", String.valueOf(admin.getPower().charAt(2)));
        model.addAttribute("l4", String.valueOf(admin.getPower().charAt(3)));


        return "admin/assign-permissions";
    }

    @RequestMapping("/assignPermissions")
    public String assignPermissions(@RequestParam("l1") String l1,
                                    @RequestParam("l2") String l2,
                                    @RequestParam("l3") String l3,
                                    @RequestParam("l4") String l4,
                                    Admin admin,
                                    Model model) {

        List<Admin> admins = adminMapper.findById(null);
        admins.removeIf(aValue -> aValue.getLevel().trim().equals("SuperAdmin"));   //去掉超级管理员
        admins.sort(Comparator.comparing(Admin::getName).reversed());
        model.addAttribute("admins", admins);
        model.addAttribute("adminId", admin.getAdminId());

        admin.setPower(l1 + l2 + l3 + l4);

        if (adminMapper.update(admin) == 1) {
            model.addAttribute("l1", l1);
            model.addAttribute("l2", l2);
            model.addAttribute("l3", l3);
            model.addAttribute("l4", l4);
            model.addAttribute("msg", "权限修改成功！！");
        } else {
            admin = adminMapper.findById(admin.getAdminId()).get(0);
            model.addAttribute("l1", String.valueOf(admin.getPower().charAt(0)));
            model.addAttribute("l2", String.valueOf(admin.getPower().charAt(1)));
            model.addAttribute("l3", String.valueOf(admin.getPower().charAt(2)));
            model.addAttribute("l4", String.valueOf(admin.getPower().charAt(3)));
            model.addAttribute("msg", "权限修改失败！！");
        }

        return "admin/assign-permissions";
    }

}
